import config from './config';
const P = config.P; //默认第几页
const SIZE = config.SIZE; //每页多少条

export default {
	/*
	 *列表初始
	 * */
	init(that = null, isReturn = false) {
		let res = {};
		res.default_p = res.p = P;
		res.size = SIZE;
		res.isLoading = true;
		res.isPage = true;
		res.notData = false;
		if (isReturn) {
			return {
				list: [],
				listTip: res
			}
		} else {
			that.list = [];
			that.listTip = res
		}
	},
    
	/*
	下拉加载更多
	*/
	onPullDownRefresh(that) {

		if (that.isRefresh) {
			return false
		}
		that.isRefresh = true;
// 		if (that.network && !that.network.isConnected) {
// 			uni.showToast({
// 				title: '当前没有网络',
// 				mask: false,
// 				icon: 'none',
// 				duration: 1500
// 			});
// 			that.isRefresh = false;
// 			uni.stopPullDownRefresh();
// 			return false;
// 		}
		let listTip = that.listTip;
		listTip.p = ++listTip.p;
		that.listTip = listTip;
		that.getList();
	},
	/*
	 *滚动加载更多
	 */
	onReachBottom(that,listTip=that.listTip) {
		/*
		if( that.network && !that.network.isConnected){
			uni.showToast({
				title: '当前没有网络',
				mask: false,
				icon:'none',
				duration: 1500
			});
			return false;
		}*/

		let p = listTip.p;
		let isPage = listTip.isPage;
		if (!isPage || listTip.notData) {
			return false
		}
		p++;
		listTip.p = p;
		console.log(p)
		that.getList(that);
	},
	/*
	 *加工数据
	 */
	normalData(list, that, listTip=that.listTip, oldList = that.list) {
        let p = listTip.p;
        let default_p = listTip.default_p;
        let size = listTip.size;
        let tip = p == default_p && list.length < size;
        listTip.isLoading = !tip;
        listTip.isPage = !tip;
        listTip.notData = false;
        return {
        	list: p > default_p ? that.isRefresh ? [...list, ...oldList] : [...oldList, ...list] : list,
        	listTip
        }
        
        
	},
	//没有数据
	notdata(that) {
		console.log('没有数据')
		let listTip = that.listTip;
		let istrue = listTip.p == listTip.default_p;
		if (istrue) {
			that.list = [];
		}
		that.listTip = {
			isLoading: false,
			notData: istrue,
			isPage: istrue,
		}
	},
    notdataReturn(listTip){
        let istrue = listTip.p == listTip.default_p;
        return {
            isLoading: false,
            notData: istrue,
            isPage: istrue,
        }
    },
	//tab切换
	tab(e, that) {
		console.log(e);
		const current = e.currentTarget.dataset.index;
		const list = this;
		if (current == that.data.current) {
			return false;
		}
		list.init(that);
		that.setData({
			current
		});
		that.getList();
	},
}
